package com.aoxon.cargo.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aoxon.cargo.R;
import com.aoxon.cargo.bean.GsonBean;
import com.aoxon.cargo.component.MyCallBack;
import com.aoxon.cargo.component.MyProgressDialog;
import com.aoxon.cargo.component.MyTextWatcher;
import com.aoxon.cargo.loader.NormalDataLoader;
import com.aoxon.cargo.service.ForgetPasswordService;
import com.aoxon.cargo.service.MyServiceFactory;
import com.aoxon.cargo.state.MyStateUtil;
import com.aoxon.cargo.util.CheckStateUtil;
import com.aoxon.cargo.util.EnvironmentUtil;
import com.aoxon.cargo.util.ToastUtil;

/**
 * 申请修改界面Activity
 * 
 * @author ZZJ
 * 
 */
public class SupForgetPasswordActivity extends BaseActivity implements
		NormalDataLoader.OnLoadDataListener {

	private EditText etAccount;
	private EditText etPhone;
	private EditText etEmail;
	private EditText etStoreName;

	private ImageView ivReturnButton;
	private ImageView ivCommitButton;

	private TextView tvTitle;

	private LinearLayout llHolder;

	private NormalDataLoader normalDataLoader = new NormalDataLoader(this);
	
	private MyProgressDialog myProgressDialog;
	private MyTextWatcher myTextWatcher;
	private ForgetPasswordService forgetPasswordService = (ForgetPasswordService) MyServiceFactory
			.getInstance(MyServiceFactory.FORGET_PASSWORD);
	private GsonBean gsonBean;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setup();
	}


	/**
	 * 自定义方法
	 * ******************************************************************************************************************************************/

	private void setup() {
		setContentView(R.layout.sup_forget_password);

		etAccount = (EditText) findViewById(R.id.etRevampAccount);
		etPhone = (EditText) findViewById(R.id.etRevampPhone);
		etEmail = (EditText) findViewById(R.id.etRevampEmail);
		etStoreName = (EditText) findViewById(R.id.etRevampStore);
		ivReturnButton = (ImageView) findViewById(R.id.ivPublicTitleReturn);
		ivCommitButton = (ImageView) findViewById(R.id.ivPublicTitleOther);
		tvTitle = (TextView) findViewById(R.id.tvPublicTiTle);
		llHolder = (LinearLayout) etAccount.getParent().getParent();

		myTextWatcher = new MyTextWatcher(new textWhatcherBack());
		myProgressDialog = new MyProgressDialog(SupForgetPasswordActivity.this);

		normalDataLoader.setOnloadDataListener(this);
		
		tvTitle.setText("忘记密码");
		ivCommitButton.setImageResource(R.drawable.button_public_commit);
		ivCommitButton.setEnabled(false);

		ivReturnButton.setOnClickListener(myClickListener);
		ivCommitButton.setOnClickListener(myClickListener);
		llHolder.setOnClickListener(myClickListener);

		etAccount.addTextChangedListener(myTextWatcher);
		etPhone.addTextChangedListener(myTextWatcher);
		etEmail.addTextChangedListener(myTextWatcher);
		etStoreName.addTextChangedListener(myTextWatcher);
	}


	/**
	 * 判断要提交的资料是否为空
	 * 
	 * @param strAccount
	 * @param strPhone
	 * @param strEmail
	 * @param strStoreName
	 * @return
	 */
	private boolean checkRevampData() {
		List<String> strList = getInputList();

		if (strList.get(0).length() >= 16) {
			etAccount.requestFocus();
			ToastUtil.show(this, "帐号不能超过16个字符");
			return false;
		}
		if (!strList.get(1).matches(
				"^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$")) {
			etPhone.requestFocus();
			ToastUtil.show(this, "请填写正确的电话号码");
			return false;
		}

		if (!strList
				.get(2)
				.matches(
						"^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$")) {
			etEmail.requestFocus();
			ToastUtil.show(this, "请填写正确的邮箱地址");
			return false;
		}
		if (strList.get(3).length() >= 24) {
			etStoreName.requestFocus();
			ToastUtil.show(this, "店铺名称不能超过24个字符");
			return false;
		}
		return true;
	}

	/**
	 * 获取所有输入框的文本列表
	 * 
	 * @return
	 */
	private List<String> getInputList() {
		List<String> strList = new ArrayList<String>();

		strList.add(etAccount.getText().toString());
		strList.add(etPhone.getText().toString());
		strList.add(etEmail.getText().toString());
		strList.add(etStoreName.getText().toString());

		return strList;
	}

	/**
	 * 判断所有输入框是否都有输入
	 * 
	 * @return
	 */
	private void checkAllInput() {
		List<String> strList = getInputList();

		for (int i = 0; i < strList.size(); i++) {
			if (strList.get(i).equals("") || strList.get(i) == null) {
				ivCommitButton.setEnabled(false);
				return;
			}
		}
		ivCommitButton.setEnabled(true);
	}


	/**
	 * 文本监听的回调
	 */
	class textWhatcherBack implements MyCallBack {

		@Override
		public void callback(String choose) {
			checkAllInput();
		}

	}

	/**
	 * 系统按键的设置
	 * *****************************************************************************************************************************************/

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			Intent intent = new Intent(SupForgetPasswordActivity.this,
					SupLoginActivity.class);
			startActivity(intent);
			this.finish();
			return false;
		}
		return false;
	}

	/**
	 * 设置各控件的点击监听
	 */
	OnClickListener myClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {

			EnvironmentUtil.closeInputMethod(SupForgetPasswordActivity.this);

			switch (v.getId()) {

			case R.id.ivPublicTitleReturn:
				ivReturnButton.setImageResource(R.drawable.back);
				Intent intent = new Intent(getBaseContext(),
						SupLoginActivity.class);
				startActivity(intent);
				SupForgetPasswordActivity.this.finish();
				break;

			case R.id.ivPublicTitleOther:
				if (checkRevampData()) {
					normalDataLoader.load();
				}
				break;

			}
		}
	};

	@Override
	public void beforeLoad() {
		// TODO Auto-generated method stub
		myProgressDialog.show("提示", "提交中...");
	}

	@Override
	public void toLoad(Message msg) throws Exception {
		// TODO Auto-generated method stub
		String strAccount = etAccount.getText().toString();
		String strPhone = etPhone.getText().toString();
		String strEmail = etEmail.getText().toString();
		String strStoreName = etStoreName.getText().toString();
		gsonBean = forgetPasswordService.execute(strAccount, strPhone,
				strEmail, strStoreName);
		msg.what = MyStateUtil.REVAMP_RESULT;
	}

	@Override
	public void afterLoad(int state) {
		// TODO Auto-generated method stub
		myProgressDialog.cancle();
		switch (state) {
		case MyStateUtil.REVAMP_RESULT:
			try {
				if (CheckStateUtil.check(SupForgetPasswordActivity.this,
						gsonBean, "提交")) {
					ToastUtil.show(getBaseContext(), "提交成功！");
					ivCommitButton.setEnabled(false);
				}
				;
			} catch (Exception e) {
				e.printStackTrace();
				myProgressDialog.cancle();
				ToastUtil.show(SupForgetPasswordActivity.this, "网络连接异常");
			}
			break;
		case MyStateUtil.NOT_NET_CONNECTION:
			ToastUtil.show(getBaseContext(), "无网络连接");
			break;
		case MyStateUtil.ERROR_NET_CONNECTION:
			ToastUtil.show(getBaseContext(), "网络连接异常");
			break;
		default:
			break;
		}
	}

}
